<?php
namespace Admin\Controller;
use Think\Controller;
class ManagerController  extends IndexController
{
    public function lists(){
        $type=I("request.type");
        if($type==1){
          $Manager=D("Manager");
          $data['username']=I("request.username");
          $data['password']=md5(I("request.password"));
          $data['who']=I("request.who");
          $data['addr']=I("request.addr");
          $data['role_id']=I("request.role_id");
          $data['shop_id']=I("request.shop_id");
          $result=$Manager->add($data);
           $this->redirect("Manager/lists");
        }
        if($type==2){
          $Manager=D("Manager");
          $id=$_COOKIE['uid'];
          $data['username']=I("request.username");
          $data['password']=md5(I("request.password"));
          $data['who']=I("request.who");
          $data['addr']=I("request.addr");
          $data['role_id']=I("request.role_id");
          $data['shop_id']=I("request.shop_id");
          $result=$Manager->where("id=".$id)->save($data);
           $this->redirect("Manager/lists");
        }


        $this->display();
    }

    //====================================获得后台用户信息列表==============================================
    public function get_manager_list()
    {
        $Manager = D('Manager');
        $result = $Manager->table('tb_manager manager,tb_shop_base base,tb_role role')
            ->where("manager.role_id=role.id and  manager.shop_id=base.id")
            ->field('manager.id,manager.username,manager.password,manager.who,manager.addr, manager.money, manager.cash,
       base.name  as shopName,role.name as roleName')->select();
            

        if (!$result) {
            echo json_encode(0);
        }
        echo json_encode($result);
    }
    //============================================用于显示增加的模板===================================================
    public function add(){
        $this-> display();
    }

    //========================================获取角色信息=======================================================

        public function role_list(){
            $Role=D("Role");
            $result=$Role->field("id,name")->select();
            if(!$result){
                echo json_encode(0);exit;
            }else{
                echo json_encode($result);
            }
        }

      ///  =====================================获取商户信息==========================================================
        public function shop_list(){
              $ShopBase=D("Shop_base");
              $result=$ShopBase->field('id,name')->select();
              if(!$result){
                  echo json_encode(0);exit;
              }else{
                  echo json_encode($result);exit;
              }

        }

        ///======================================删除商户信息===============================================
        public function del(){
          $Manager=D("Manager"); 
          $id=I("request.id");
          $map['id']=$id;
          $result=$Manager->where($map)->delete();
           if($result){
              echo json_encode(1);exit;
           }else{
              echo json_encode(0);exit;
           }
        }

        //============================修改==================================
        public function update(){
          $id = I("request.id");
          setcookie("uid", $id, time() + 3600);
          $this->display();
        }
        //=========================获取修改用户的信息===================================
                   
         public function update_list(){
          $Manager=D("Manager");
          $id = $_COOKIE["uid"];
          $where="id=".$id;
          $result=$Manager->where($where)->find();
          if(!$result){
              echo json_encode(0);exit;
            }else{
              echo json_encode($result);exit;
            }
         }


        //========================获取角色列表===========================================
       public function  update_role_list(){
          $Role=D("Role");
          $result=$Role->field('id,name')->select();
          if(!$result){
            var_dump($result);
            echo json_encode(0);exit;
          }else{
            echo json_encode($result);exit;
          }
       }

         //========================获取角色列表===========================================
       public function  update_shop_list(){
          $ShopBase=D("Shop_base");
          $result=$ShopBase->field('id,name')->select();
          if(!$result){
            echo json_encode(0);exit;
          }else{
            echo json_encode($result);exit;
          }
       }


}
